package com.war.controller.safeManage;

import com.war.pojo.system.User;
import com.war.service.safeManage.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@Controller
@RequestMapping("/index")
public class IndexController {
    @Autowired
    UserService userService;

    //访问welcome页面
    @RequestMapping("/welcome")
    public String welcome(HttpServletRequest request){
//        //验证登录状态
//        if (userService.checkLogin(request) != true){
//            return "safeManage/login";
//        }
//        else {
            return "safeManage/welcome";
//        }
    }

    //访问 index页面
    @RequestMapping("/homePage")
    public String index(HttpServletRequest request){
//        //验证登录状态
//        if (userService.checkLogin(request) != true){
//            return "safeManage/login";
//        }
//        else {
            return "safeManage/index";
//        }
    }

    //获取用户的身份
    @RequestMapping("/identity")
    @ResponseBody
    public HashMap<String,Object> identity(HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<String,Object>();
        String info = userService.checkIdentity(request);
        map.put("identity",info);
        return map;
    }

    //访问修改信息页面
    @RequestMapping("/updateUserPage")
    public String updateUser(User user, ModelMap m, HttpServletRequest request){
//        //验证登录状态
//        if (userService.checkLogin(request) != true){
//            return "safeManage/login";
//        }
        User u = userService.selectByTel(user);
        //把数据传到前端
        m.addAttribute("user",u);
        return "safeManage/updateUser";
    }

    //处理修改信息的Ajax请求
    @RequestMapping("/updateUser")
    @ResponseBody
    public HashMap<String,Object> updateUser(User user, HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<String,Object>();
        String info = userService.updateUser(user,request);
        map.put("info",info);
        return map;
    }

    //注销登录
    @RequestMapping("/logOut")
    @ResponseBody
    public HashMap<String,Object> logOut(HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<String,Object>();
        String info = userService.logOut(request);
        map.put("info",info);
        return map;
    }

}
